package edu.sctu2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class Select2 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC&characterEncoding=gbk",
                    "root",
                    "w19971201t");
            statement=connection.createStatement();
            resultSet=statement.executeQuery("SELECT * FROM t_user2 WHERE id="+id+"");
            if (resultSet.next()){
                req.setAttribute("username",resultSet.getString(2));
                req.setAttribute("password",resultSet.getString(3));
                req.getRequestDispatcher("/message.jsp").forward(req,resp);
            }
            else {

                req.setAttribute("username","null");
                req.setAttribute("password","null");
                req.getRequestDispatcher("/message.jsp").forward(req,resp);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }




    }
}
